<?php


namespace backend\models;
use backend\models\Order;
use yii\db\ActiveRecord;
use Yii;
class Total extends ActiveRecord
{
    public function today()
    {

    }

    public static function executes($data)
    {
        $hour = "";
        $totalprice = "";
        foreach ($data as $key => $value) {

            $hour .= $value['hour'] . ',';
            $totalprice .= $value['totalprice'] . ',';

        }

        $arr['hour'] = explode(',', $hour);
        $arr['totalprice'] = explode(',', $totalprice);
        array_pop($arr['hour']);
        array_pop($arr['totalprice']);

        return $arr;
    }

    public static function getSql($starttime, $type)
    {
        if ($type == null) {
            $tomorrow=strtotime($starttime)+86400;

            $endtime = date("Y-m-d", $tomorrow);

            $sql = "SELECT DATE_FORMAT(FROM_UNIXTIME(order_time), '%H') as hour,SUM(order_price) as totalprice
from `order`
 where order_time >= UNIX_TIMESTAMP('$starttime.00:00:00')
and order_time < UNIX_TIMESTAMP('$endtime. 00:00:00')
and order_status=1

GROUP BY DATE_FORMAT(FROM_UNIXTIME(order_time),'%Y-%m-%d %H')";
        } elseif($type=='month') {
            $year=substr($starttime,0,5);
            $month=substr($starttime,5,2);
            $month=$month+1;
            $sql="SELECT DATE_FORMAT(FROM_UNIXTIME(order_time), '%d') as hour,SUM(order_price) as totalprice
from `order`
 where order_time >= UNIX_TIMESTAMP('$starttime.-01 00:00:00')
and order_time < UNIX_TIMESTAMP('$year.$month.-01 00:00:00')
and order_status=1

GROUP BY DATE_FORMAT(FROM_UNIXTIME(order_time),'%Y-%m-%d %d')";
        }else
        {
            $year=substr($starttime,0,5);
            $month=substr($starttime,5,2);
            $month=$month+1;
            $sql="SELECT DATE_FORMAT(FROM_UNIXTIME(order_time), '%d') as hour,SUM(order_price) as totalprice
from `order`
 where order_time >= UNIX_TIMESTAMP('$starttime.-01 00:00:00')
and order_time < UNIX_TIMESTAMP('$year.$month.-01 00:00:00')
and order_status=1

GROUP BY DATE_FORMAT(FROM_UNIXTIME(order_time),'%Y-%m-%d %d')
ORDER BY totalprice desc limit 15";
        }
        $data = Yii::$app->db->createCommand($sql)->queryAll();
        $data = self::executes($data);
        $data['time']=$starttime;
        return $data;
    }
    public static function get_top(){
        $sql="select user_id , username as hour, COUNT(*) as totalprice from `order`  as a

LEFT  JOIN `user` as b
on 
a.user_id=b.id
GROUP BY a.user_id
ORDER BY totalprice
 LIMIT 10";
      $data=Yii::$app->db->createCommand($sql)->queryAll();
        $data = self::executes($data);
       return $data;
    }
}